<?php
/**
 * 收货地址精度类
 * @package  htdocs
 * @subpackage  module/base
 * @author   create by allen.qiang
 * @version 1.0
 */

class addressPrecision{

    /**
     * 查询地址组信息
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return null|array
     */
    public function search( &$arg_array ) {
        
        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //地址
        if (isset($arg_array['address']) && !empty($arg_array['address'])) {
            $sql .= ' AND address LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['address'] . '%';
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4) ;
        }



        try {

            if (isset($arg_array['rp'])) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_address_precision  ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }


            //查询备注信息
            $search_sql = 'SELECT * FROM sys_address_precision ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach ($temp_item_array as &$item) {

                $item_array[] = array(
                    'address' => $item->address,
                    'md5_address' => $item->md5_address,
                    'longitude' => $item->longitude,
                    'latitude' => $item->latitude,
                    'addr_precision' => $item->addr_precision
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 新增/修改地址
     * @param array $arg_array 地址参数数组
     * @return array
     */
    public function editAddress( $arg_array ) {

        global $G_SHOP;

        if (!isset( $arg_array['md5_address'], $arg_array['longitude'], $arg_array['latitude'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }


        try {

            $addressPrecision = new entity('sys_address_precision');

            $addressPrecision->md5_address = $arg_array['md5_address'];
            if (!$G_SHOP->DBCA->fetch($addressPrecision)) {
                throw new Exception('地址信息没有找到', 404);
            }

            $addressPrecision->longitude = $arg_array['longitude'];
            $addressPrecision->latitude = $arg_array['latitude'];

            $G_SHOP->DBCA->save($addressPrecision);
            return array( 'code' => 200, 'message' => '地址修改成功' );

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 批量删除地址信息
     * @param array $arg_array 地址组参数数组
     * @return array
     * @throws Exception
     */
    public function deleteAddress ( $arg_array ) {

        global $G_SHOP;

        if (!isset($arg_array) || !is_array($arg_array) || empty($arg_array)) {
            return array( 'code' => 400, 'message' => '请选择要删除的地址' );
        }

        try {

            foreach($arg_array as &$md5_address) {

                //检查地址信息
                $address_obj = new entity('sys_address_precision');
                $address_obj->md5_address = $md5_address;
                if (!$G_SHOP->DBCA->fetch($address_obj)) {
                    continue;
                }
                $G_SHOP->DBCA->delete( $address_obj );

            }

            return array( 'code' => 200, 'message' => '删除成功' );

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

}